local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();
local TeleportGui=UIManager:CreateUI("TeleportGui",require(script.TeleportGui))
local Chapter2level_Cfg=_G.CommonOHOH.config.Chapter.chapter2level

local ConnectClass=_G.CommonOHOH.load.Connect();
local ItemTool=_G.CommonOHOH.load.ItemTool();
local Tool=require(script.Parent.Tool)
function  FindThumbnailByBattlePerfabName(name)
    local s=string.format("Battle.normal.%s.thumbnail",name)
   return  ClientOHOH:GetPath_Loop(s,workspace).Image
end


function M.CreateTeleportData(self)
	local data={
	}

	for i,v in ipairs(Chapter2level_Cfg) do
		local minLevel=v.levelRange[1]
        local maxLevel=v.levelRange[2]

		table.insert(data,{
            thumbnail=FindThumbnailByBattlePerfabName(v.perfab),
			title1=_G.LanguageHelper:GetText(v.name),
			title2=_G.LanguageHelper:GetText2(888015,{i}),
			title3=_G.LanguageHelper:GetText2(888012,{minLevel,maxLevel}),

			isCanUnlock= false,--是否可解锁传送
			isCanTeleport=false, -- 是否能够传送
			lockTip={
                [1]=_G.LanguageHelper:GetText2(888014,{v.condition[1]}), --击败boss数
				[2]=_G.LanguageHelper:GetText2(888013,{v.condition[2]}),--重生数
			},
			unlockCost=v.unlockCost,
			id=i,
			unlockEvent=ConnectClass.new(),
			updEvent=ConnectClass.new(),
			teleportEvent=ConnectClass.new(),

			updState=function(this,dataStore)
               
                if this.isCanTeleport then --已经可以传送，直接返回
                    return 
                end

                local chapter=dataStore:GetData("chapter")

                if chapter[this.id]==2 then --已经可以传送
                    this.isCanTeleport=true
                    this.isCanUnlock=true
                elseif Tool.IsCanUnLock(this.id,dataStore) then --已经可以解锁
                    this.isCanUnlock=true
                end
                this.updEvent:Action()
			end
		})
	end

	return data
end

function M.new(self,...)
	return M(self,...);
end;



function M.ctor(self, parent)
	M.super.ctor(self, parent);


    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())


    self.ChapId=1

	self.TeleportData=self:CreateTeleportData()

	self.TeleportCFrame={} --缓存传送位置
	-- self.TeleportSky={} --缓存天空盒

    self.LastLevel=self.DataStore:GetData("base").level
	for _,v in pairs(self.TeleportData) do

        v:updState(self.DataStore)
		v.teleportEvent:Connect(function(...)
            -- print("-----teleportEvent-----------")
			self:Teleport(...)
		end)
		v.unlockEvent:Connect(function(...)
            -- print("-----unlockEvent-----------")
			self:Unlock(...)
		end)

		local touch=	ClientOHOH:GetPath_Loop(v.id..".Touch",workspace.Telepot)
		self.TeleportCFrame[v.id]=ClientOHOH:GetPath_Loop("go",touch).WorldCFrame

		self:AddTouchEvent(touch)

	end


    self.parent.UserDataEvent:Connect(function(data)
        if data["base"]  or data["levels"] or data["chapter"] then
            for _,v in pairs(self.TeleportData) do
                v:updState(self.DataStore)
            end
        end
        
    end)

	TeleportGui:InitData(self.TeleportData)

	
end;

function M.dtor(self)
	M.super.dtor(self);
end;



--获取玩家所在章节
function M.GetActiveChapId(self)


    return self.ChapId
end


--更新天空盒
function M.UpdateSky(self,chapId)
   local skyFolder=  _G.client:GetComponent(_G.CommonOHOH.load.ReayBattleCtrl()):GetSkyFolder(chapId)
   self.SkyCtrl=  self.parent:GetComponent(ClientOHOH.load.Client_SkyComponent())
    if #skyFolder:GetChildren()>0 then
        self.SkyCtrl:SetSky(skyFolder:Clone())
    else
        self.SkyCtrl:SetDefaultSky()
    end
end

--开始传送
function M.Teleport(self,data)
     self.ChapId=data.id
     self:UpdateSky(self.ChapId)
	 self.parent.character:SetCFrame(self.TeleportCFrame[data.id])
	 TeleportGui:Hide()
end

--购买解锁--->
local FunctionList = ClientOHOH.load.FunctionList();

function M.Unlock(self,data)
    if data.isCanUnlock then
        -- print("---------1-------")
        if ItemTool.IsHadEnoughByTable(data.unlockCost,self.DataStore) then
            -- print("---------3------")
            if self:Invoke(FunctionList.unLock_chapter,data.id) then
                -- print("--------5-----")
                _G.GlobalTipGui:Show( _G.LanguageHelper:GetText(8888003))
            end
        else
            -- print("---------4-----")
            _G.GlobalTipGui:Show( _G.LanguageHelper:GetText(8888004))
        end
    else
        -- print("---------2------")
        _G.GlobalTipGui:Show( _G.LanguageHelper:GetText(8888001))
    end
end

local CallBackManager = _G.CommonOHOH.load.CallBackManager();

function M.AddTouchEvent(self,enterTouch)

    CallBackManager:AddRbxConnect(self,enterTouch.Touched, function(obj,chPart)
        if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart) then
			return 
		end
        TeleportGui:Show()
    end);

    CallBackManager:AddRbxConnect(self,enterTouch.TouchEnded, function(obj,chPart)
        if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart) then
			return 
		end
		TeleportGui:Hide()
    end);

end
return M